 /*
 * 文章管理视图
 * @Author: LuoQianJun
 * @Date: 2018-01-25 15:36:18
 * @Last Modified by: LuoQianJun
 * @Last Modified time: 2018-02-09 11:35:46
 */

<style lang="less" rel="stylesheet/less" type="text/less" scoped>

    .v-admin-article-views {
        padding: 20px;
    }

</style>

 <template>

    <div class="v-admin-article-views">

        <!-- 面包屑组件 -->
        <vBreadcrumb :breadcrumbList="breadcrumbList"></vBreadcrumb>

        <Tabs type="card" @on-click="changeTab">

            <TabPane label="新增" name="新增">
                
                <article-create ref="create"
                                :article-types="article_types"
                                @post="postData">


                </article-create>


            </TabPane>


            <TabPane label="查看" name="查看">

                <search-form  @search="confirmSearch"
                              :article-types="article_types">
                </search-form>

                <article-index :loading="table_loading"
                               :articles="articles.data"
                               :article-types="article_show_types"
                               @edit="getArticleDetail"
                               @delete="deleteArticle">

                </article-index>
                
                <br>

                <!-- 分页组件 -->
            
                <Row type="flex" justify="center">
                    <Page @on-change="pageChange" 
                        @on-page-size-change="perPageChange"
                        placement="top"
                        :total="articles.total" 
                        :current="articles.current_page"
                        :page-size="query_info.per_page"
                        show-sizer show-total></Page>
                </Row>
                                                           
            </TabPane>
        </Tabs>

        <Modal  title="编辑文章信息"
                width="768"
                v-model="show_modal">

            <!-- 产品编辑组件 -->
            <article-edit  :article-types="article_types"
                           :article-detail="article_detail"
                            @update="updateArticle"></article-edit>

            <div slot="footer"></div>

        </Modal>

    </div>

 </template>
 
 <script>

import API_LIST from 'src/API_LIST';

import vBreadcrumb from 'src/views/component/breadcrumb';

import { mapGetters , mapMutations } from "vuex";

import qs from 'qs';

import articleCreate from './create';

import articleIndex from './index';

import articleEdit from './edit';

import searchForm from './search-form';

export default {

     name: 'article-new',

      components : {

        vBreadcrumb,

        articleCreate,

        articleIndex,

        articleEdit,

        searchForm
 
    },

    data: function() {	

         return {

            breadcrumbList: [
                {
                    name : '管理员',
                    href : '#',
                },
                {
                    name : '文章管理',
                    href : '#'
                },
                {
                    name : '新建文章',
                    href : '#',
                }
            ],

            query_info : {

                page : 1,

                per_page : 10,

                article_type_id : '',

                title : '',


            },
            
            search_info : {

                page : 1
                
            },                      //分页信息

            table_loading : false,      //表格更新时的loading

            show_modal : false,         //弹框的控制

            articles : {},               //文章信息

            article_show_types : {},    //列表展示所需的文章类别

            article_types : [],         //创建、编辑、搜索框的文章类别

            article_detail : {},        //文章详情


         }
    },

    mounted () {
    
        this.fetchArticleTypes();
        
    },

    computed: {
        ...mapGetters('userToken',[
            'getToken'
        ])  
    },

    methods: {

        ...mapMutations('spinShow',[
            'showSpin',
            'closeSpin'
        ]),

         /**
         * 切换tab
         * @param  {tab_name} tab_name {tab的name}
         * @return {undefined} 
         */
        changeTab(tab_name) {
            
            if( tab_name === '查看' ) {

                this.fetchArticleList(this.query_info);

            }else {

                this.articles.data = [];

            }

        },

        /**
         * 页码改变
         * @param  {type} pageNumber {当前页码}
         * @return {undefined} 
         */
        pageChange(pageNumber) {

            this.search_info.page = pageNumber;

            this.search_info.per_page = this.query_info.per_page;

            this.fetchArticleList(this.search_info);
        },


        /**
         * 每页显示数改变
         * @param  {type} per_page {当前每页显示数}
         * @return {undefined}
         */
        perPageChange(per_page) {

            this.search_info.per_page = per_page;

            this.query_info.per_page = per_page;

            this.fetchArticleList(this.search_info);

        },
 
        /**
         * 确认搜索
         * @param  {Object} param {搜索的参数对象}
         * @return {undefined}
         */
        confirmSearch(param) {

            this.query_info.article_type_id = param.article_type_id;

            this.query_info.title = param.title;

            this.search_info = JSON.parse(JSON.stringify(this.query_info));

            this.fetchArticleList(this.query_info);

        },

        /**
         * 查看文章详情
         * @param  {string} id { 文章的id}
         * @return {undefined}
         */

        getArticleDetail(id){

            this.article_detail = this.articles.data.filter((article) => article.id === id)[0];

            this.show_modal = true;

        },

        /**
         * 获取文章类别
         * @return {undefined}
         */
        fetchArticleTypes(){

            this.showSpin('请稍后');

            this.$http.get( API_LIST.ARTICLE  + '/create' )
                    .then(({ data })=>{
                                
                        if( parseInt(data.code) !== 0 )throw new Error(data.error);

                        this.article_types = data.article_types;

                        this.article_show_types = data.types;
                                
                    })
                    .catch((err)=>{
                        this.$Message.error(err.message);
                    })
                    .then(this.closeSpin)

        },


        /**
         * 获取文章列表
         * @param  {Object} param {搜索列表的参数}
         * @return {undefined}
         */
        fetchArticleList(param){

            this.table_loading = true;

            let query_str = qs.stringify(param);

            this.$http.get( API_LIST.ARTICLE + '?' + query_str )

                .then(({ data })=>{
                            
                    if( parseInt(data.code) !== 0 )throw new Error(data.error);
                    
                    this.articles = data.articles;
                                         
                })

                .catch((err)=>{
            
                    this.$Message.error(err.message);

                })

                .then(() => this.table_loading = false)

        },


        /**
         * 新增文章的post数据
         * @return {undefined}
         */
        postData(param){

            this.showSpin('请稍后');

            this.$http.post( API_LIST.ARTICLE , param )
                    .then(({ data })=>{
                                
                        if( parseInt(data.code) !== 0 )throw new Error(data.error);

                        this.$Message.success('新增文章成功！');

                        this.$refs.create.handleReset('formValidate');
                                
                    })
                    .catch((err)=>{

                        this.$Message.error(err.message);
                
                    })
                    .then(this.closeSpin)

        },

        /**
         * 删除文章
         * @param  {string} id {需要删除的文章的id}
         * @return {undefined}
         */
        deleteArticle(id){

            this.$Modal.confirm({

                title: '确定删除该篇文章吗？',

                onOk: () => {

                    const param = {

                        _method : 'DELETE'

                    }

                    this.table_loading = true;

                    this.$http.post( API_LIST.ARTICLE + '/' + id , param )
                        .then(({ data })=>{
                                    
                            if( parseInt(data.code) !== 0 )throw new Error(data.error);

                            this.fetchArticleList(this.search_info);

                            this.$Message.success('删除成功');
                                    
                        })
                        .catch((err)=>{
                    
                            this.$Message.error(err.message || err.statusText);

                        })
                        .then(() => this.table_loading = false)
                    
                },
                onCancel: () => {}
            });

        },


        /**
         * 更新文章信息
         * @param  {Object} param {需要的参数对象}
         * @return {undefined}
         */
        updateArticle(id , param){

            this.showSpin();

            this.$http.post( API_LIST.ARTICLE + '/' + id, param )

                .then(({ data })=>{
                            
                    if  ( parseInt(data.code) !== 0 )throw new Error(data.error);

                    this.$Message.success('文章信息更新成功！');

                    this.show_modal = false;

                    this.fetchArticleList(this.search_info);
                            
                })
                .catch((err)=>{

                    this.$Message.error(err.message || err.statusText);
            
                })
                
                .then(this.closeSpin)

        },

    }
}
</script>
